package com.atguigu.lease.web.admin.controller.apartment;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.LabelInfo;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.service.LabelInfoService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "标签管理")
@RestController //@Controller  + @ResponseBody
@RequestMapping("/admin/label")
public class LabelController {


    @Autowired
    LabelInfoService labelInfoService;

    @Operation(summary = "新增或修改标签信息")
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody LabelInfo labelInfo) { //涉及 type类型转换为枚举对象。
        labelInfoService.saveOrUpdate(labelInfo);
        return Result.ok();
    }

    @Operation(summary = "根据id删除标签信息")
    @DeleteMapping("/deleteById")
    public Result deleteById(@RequestParam Long id) {
        labelInfoService.removeById(id);
        return Result.ok();
    }

    //   http://localhost:8080/admin/label/list?type=1
    //controller方法上增加@ResponseBody注解。底层HttpMessageConverter
    // MappingJackson2HttpMessageConverter  方法返回结果是JavaBean，会使用Jackson组件，将Bean对象序列化为Json串。
    // StringHttpMessageConverter  方法返回结果是字符串类型，原样输出
    @Operation(summary = "根据类型）查询标签列表")
    @GetMapping("/list")
    public Result<List<LabelInfo>> findLabelInfoList(@RequestParam(value="type",required = false) ItemType type){
        //Wrapper  抽象类      子类常用： LambdaQueryWrapper  LambdaUpdateWrapper   QueryWrapper   UpdateWrapper
        LambdaQueryWrapper<LabelInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(type!=null,LabelInfo::getType,type);
        List<LabelInfo> list = labelInfoService.list(queryWrapper);
        return Result.ok(list);
    }
}
